package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;



@SuppressWarnings("serial")
public class MainMenu extends JPanel implements KeyListener
{
	private ArrayList<JButton> buttons = new ArrayList<JButton>();
	private Main main;
	private int selectedButton = 0;
	
	
	public MainMenu(Main m)
	{
		main = m;
		buttons.add(new JButton("Infinite Snake"));
		buttons.add(new JButton("Level Editor"));
		buttons.add(new JButton("Custom Level Select"));
		buttons.add(new JButton("Level Select"));
		//buttons.add(new JButton("High Scores"));
		JLabel welcome = new JLabel("Welcome to SNAKE. \n Use the arrow keys to choose an option.");
		welcome.setHorizontalAlignment(SwingConstants.CENTER);
		add(welcome);
		setLayout(new GridLayout(buttons.size()+1, 0));
		for(int i=0; i<buttons.size(); i++)
		{
			for(int j=0; j<buttons.size(); j++)
			{
				buttons.get(j).setEnabled(false);
				if(j == selectedButton)
				{
					buttons.get(j).setBackground(Color.blue);
					buttons.get(j).setFont(new Font("Arial", 1, 30));
				}
				else
				{
					buttons.get(j).setBackground(Color.green);
					buttons.get(j).setFont(new Font("Arial", 1, 15));
				}
			}
			add(buttons.get(i));
		}
		setFocusable(true);
		this.validate();
		
		addKeyListener(this);
		switchButton();
		validate();
		
		
	}
	
	private void switchButton() 
	{
		for(int i=0; i<buttons.size(); i++)
		{
			if(i == selectedButton)
			{
				buttons.get(i).setBackground(Color.blue);
				buttons.get(i).setFont(new Font("Arial", 1, 30));
			}
			else
			{
				buttons.get(i).setBackground(Color.green);
				buttons.get(i).setFont(new Font("Arial", 1, 15));
			}
		}
		
		revalidate();
	}

	public void keyPressed(KeyEvent e) 
	{}

	
	public void keyReleased(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_UP)
		{
			if(selectedButton != 0)
			{
				selectedButton--;
				switchButton();
			}
			else
			{
				selectedButton = buttons.size()-1;
				switchButton();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			if(selectedButton != buttons.size()-1)
			{
				selectedButton++;
				switchButton();
			}	
			else
			{
				selectedButton = 0;
				switchButton();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			hitButton(selectedButton);
		}
	}

	
	private void hitButton(int i)
	{
		main.mainMenuChooseItem(i);
		removeKeyListener(this);
		setFocusable(false);
	}



	public void keyTyped(KeyEvent e) 
	{}
}